package jman.feb03;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TestJsp
 */
public class TestJsp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestJsp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("do Get:"+request.getParameter("getvalue"));
		//9大jsp内置对象：1.request范围：用户当前提交的请求
		System.out.println("request.getContextPath()=="+request.getContextPath());
		System.out.println("request.getRemoteAddr()=="+request.getRemoteAddr());
		System.out.println("request.getRequestedSessionId()=="+request.getRequestedSessionId());
		System.out.println("request.getProtocol()=="+request.getProtocol());
		System.out.println("request.getRemoteHost()=="+request.getRemoteHost());
		//2.response 对客户端的响应。向客户端发送数据
		response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setCharacterEncoding("utf-8");
		response.getWriter().append("登录成功");
		//重定向的方式做页面跳转。相当于，直接让浏览器访问另外一个地址，浏览器地址是变的（即在sevlet里设置的值不能传到前端，但sessio不会消失）
		response.sendRedirect("");
		//请求转发的方式做页面跳转。相当于，浏览器的地址不变，只是浏览器加载的内容，变成了新的地址的内容
		request.getRequestDispatcher("").forward(request, response);
		//3.session:只要客户不关闭浏览器，，session里面存的值就一直存在。
		HttpSession as=request.getSession();
		String name="cx";
		as.setAttribute("name", name);
		//application 服务器启动。就会创建一个application对象，只要服务器没有关闭，里面的值就一直在。
		ServletContext application=this.getServletContext();
		//application.setAttribute("test", 100);最好别用
		//5.Out 向客户端发送数据。
		PrintWriter out=response.getWriter();
		//6.cookie
		Cookie ck=new Cookie("a","100");
		int week=1000*60*60*24*7;
		ck.setMaxAge(week);
		response.addCookie(ck);
		//7.config
		
		//8.Page(PageContext)
		
		//9.Exception
		
		//四大作用域
		
		//1.page,里面存的值只能作用于当前页面，只要发生页面跳转，里面的值就没了。
		//2.request，里面的值可以跨越forward的前后两个页面。只要刷新页面，里面的值就没了。
		//3.session,只要不关闭浏览器，里面存的值就在，一定拿关闭浏览器，里面的值就不存在。
		//4.application,只要不关闭服务器，里面的值就一直在，一旦关闭服务器，里面的值就没了。
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("do Post:"+request.getParameter("postvalue"));
		doGet(request, response);
	}

}
